package com.bridge.processor.init;

import com.bridge.processor.init.cache.CacheManager;
import com.bridge.processor.init.cache.LoadLoadPropertiesFromServer;
import com.bridge.consistency.ThreadContext;
import com.bridge.domain.BridgeConfig;
import com.bridge.listener.PropertiesChangeListenerHolder;
import com.bridge.processor.init.cache.container.LocalCacheHolder;
import com.bridge.processor.init.cache.container.MirrorCacheHolder;
import com.bridge.processor.init.config.ConfigManager;
import com.bridge.processor.init.config.ZookeeperConfigStrategy;
import com.bridge.zookeeper.ZookeeperClient;

/**
 * @author Jay
 * @version v1.0
 * @description 环境的控制
 * @date 2019-01-14 11:18
 */
public class Environment {


    /**
     * 初始化
     *
     * @param bridgeConfig
     */
    public static void init(BridgeConfig bridgeConfig) {
        // 实例重载策略初始化
        ConfigManager.getInstance().init(new ZookeeperConfigStrategy(), bridgeConfig).onConfigPrepared();
        // 本地缓存池初始化
        CacheManager.getInstance().init(new LoadLoadPropertiesFromServer(), bridgeConfig).onCacheInit();
    }


    /**
     * 关闭资源
     */
    public static void destroy() {
        // 关闭zk连接
        ZookeeperClient.getInstance().closeZooKeeperConnection();
        // 清空本地缓存
        LocalCacheHolder.clearLocalCacheHolder();
        // 清空镜像缓存
        MirrorCacheHolder.clearMirrorCache();
        // 清空线程上下文
        ThreadContext.removeMirrorVersion();
        // 清空监听器
        PropertiesChangeListenerHolder.clearPropertiesChangeListener();
    }


}
